package com.cn.yunhe.rentcars.web;

import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.InsuranceMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.Insurance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
*
* @author 娄鹏欣
* @version 1.0.0
*
*/
@RestController
public class InsuranceController  {
	
	@Autowired
	private InsuranceMapper insuranceMapper;
	@Autowired
	private CarMapper carMapper;
	//url:insurance-list返回insurance-list页面
	@RequestMapping(value="/insurance-list", method=RequestMethod.GET)
	public ModelAndView insuranceList() {	
		return new ModelAndView("insurance-list");
	}
	//url:insurance-add返回insurance-add页面
	@RequestMapping(value="/insurance-add", method=RequestMethod.GET)
	public ModelAndView insuranceAdd() {	
		return new ModelAndView("insurance-add");
	}
	//url:insuranceedit返回insurance-edit页面
	@RequestMapping(value="/insuranceedit", method=RequestMethod.GET)
	public ModelAndView insuranceAlter(@RequestParam(required=false) String recordid) {	
		System.out.print(recordid);
		Insurance insurance = insuranceMapper.getInsuranceById(recordid);
		
		Map<String,Insurance> modelMap = new HashMap<String,Insurance>();
		modelMap.put("insurance", insurance);
		
	return new ModelAndView("insurance-edit",modelMap);		
	}
	@RequestMapping(value="/getInsuranceBycarid", method=RequestMethod.GET)
	public PageInfo<Insurance> insuranceByName(@RequestParam String carid,@RequestParam  int pageNum,@RequestParam  int pageSize){
	      PageHelper.startPage(pageNum, pageSize);
	      List<Insurance> InsuranceList = insuranceMapper.getInsurance(carid);
	      PageInfo<Insurance> page = new PageInfo<Insurance>(InsuranceList);
	      return page;
	}
	
	/**
	 * 模块：车辆保险记录
	 * 功能：添加新保险记录
	 * @param Insurance
	 * @author 娄鹏欣
	 */
	@RequestMapping(value="/addInsurance", method=RequestMethod.POST)
	public void addInsurance(@RequestBody Insurance insurance ) throws ParseException{					
		insurance.setRecordid(UUID.randomUUID().toString());				
		insuranceMapper.addInsurance(insurance);
		
		Car car = new Car () ;
		car.setCarcode(insurance.getCarid());
		car.setInsurancetime(insurance.getInvaliddate());
		carMapper.setCarAllstate(car);
		
	}
	
	/**
	 * 模块：车辆保险记录
	 * 功能：修改保险记录
	 * @param recordid
	 * @author 娄鹏欣
	 */
	@RequestMapping(value="/insurance/{recordid}", method=RequestMethod.PUT)
	public void editInsurance(@PathVariable("recordid") String recordid,@RequestBody Insurance insurance){
		insurance.setRecordid(recordid);
		insuranceMapper.editInsurance(insurance);
		//修改Insurance联动修改关联的车辆保险信息		
		Car car = new Car () ;
		car.setCarcode(insurance.getCarid());
		car.setInsurancetime(insurance.getInvaliddate());		
		carMapper.setCarAllstate(car);
	}
	
	/**
	 * 模块：车辆保险记录
	 * 功能：删除保险记录
	 * @param recordid
	 * @author 娄鹏欣
	 */
	@RequestMapping(value="/delInsurance", method=RequestMethod.POST)
	public void delInsurance(@RequestParam String recordid){ 
		System.out.println(recordid);
		insuranceMapper.delInsurance(recordid);
	}
	
	/**
	 * 模块：车辆保险记录
	 * 功能：批量修改保险记录
	 * @param recordids
	 * @author 娄鹏欣
	 */
	@RequestMapping(value="/delInsureances", method=RequestMethod.POST)
	public void delInsurances(@RequestParam String recordid){ 
		System.out.println(recordid);
		List<String> recordidList = Arrays.asList(recordid.split(","));		
		insuranceMapper.delInsurances(recordidList);				
	}
	
	/**
	 * 模块：车辆保险记录
	 * 功能：模糊查询保险记录
	 * @param carid
	 * @param draw
	 * @param start
	 * @param length
	 * @return insuranceList
	 * @author 娄鹏欣
	 */
	@RequestMapping(value="/insurance", method=RequestMethod.GET)
	public Map<String,Object> getLikeInsurance(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String carid,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  //为向配置文件传map赋值
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("carid", carid);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		  //分页查询
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 		  
	      PageHelper.startPage(pageNum, pageSize);
	      List<Insurance> insuranceList = insuranceMapper.searchInsurance(params);
	      PageInfo<Insurance> page = new PageInfo<Insurance>(insuranceList);
	      
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}
	
}
